package com.gzy.net_app;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.gzy.net_app.databinding.ActivityMain2Binding;
import com.gzy.net_app.ui.BaseActivity;
import com.gzy.net_app.ui.activity.PublishActivity;
import com.gzy.net_app.ui.fragment.HomeFragment;
import com.gzy.net_app.ui.fragment.MeFragment;
import com.gzy.network.NetworkApi;
import com.gzy.net_app.api.HttpService;
import com.gzy.net_app.bean.WallPaperResponse;
import com.gzy.network.observer.BaseObserver;
import com.gzy.network.utils.KLog;
import java.util.List;

public class MainActivity extends BaseActivity<ActivityMain2Binding> {

  // //点击6次
  // private final int CLICK_NUM = 6;
  // //点击时间间隔5秒
  // private final int CLICK_INTERVAL_TIME = 5000;
  // //上一次的点击时间
  // private long lastClickTime = 0;
  // //记录点击次数
  // private int clickNum = 0;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    // binding.image.setOnClickListener(view -> sixClick());
    // binding.button1.setOnClickListener(view -> startActivity(new Intent(this, GetTextActivity.class)));

    // requestNetwork();

    binding.mainTitle.setText("作品详情");
    if (savedInstanceState == null){
      getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout,new HomeFragment()).commit();
    }

    binding.mainAddImage.setOnClickListener(view -> startActivity(new Intent(this, PublishActivity.class)));
    initviewpager();
  }

  private void initviewpager() {

    //获取底部导航图标颜色，根据图标颜色设置文字颜色
    Resources resource = getResources();
    @SuppressLint("ResourceType")
    ColorStateList csl = resource.getColorStateList(R.drawable.bottom_navigation_item_selector);

    binding.bottomNavigationView.setItemTextColor(csl);


    binding.bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
      @Override
      public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment fragment=null;
        switch (item.getItemId()){
          case R.id.navigation1:
            binding.mainTitle.setText("作品详情");
            fragment=new HomeFragment();
            break;
          // case R.id.navigation2:
          //   binding.mainTitle.setText("发布作品");
          //   fragment=new PublishFragment();
          //   break;
          case R.id.navigation3:
            binding.mainTitle.setText("我的");
            fragment=new MeFragment();
            break;
        }
        getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout,fragment).commit();
        return true;
      }
    });
  }



  // /**
  //  * 连续6次点击
  //  */
  // public void sixClick() {
  //   //点击的间隔时间不能超过5秒
  //   long currentClickTime = SystemClock.uptimeMillis();
  //   if (currentClickTime - lastClickTime <= CLICK_INTERVAL_TIME || lastClickTime == 0) {
  //     lastClickTime = currentClickTime;
  //     clickNum = clickNum + 1;
  //   } else {
  //     //超过5秒的间隔
  //     //重新计数 从1开始
  //     clickNum = 1;
  //     lastClickTime = 0;
  //     return;
  //   }
  //   if (clickNum == CLICK_NUM) {
  //     //重新计数
  //     clickNum = 0;
  //     lastClickTime = 0;
  //     /*实现点击多次后的事件*/
  //     Toast.makeText(MainActivity.this, "设置网络环境", Toast.LENGTH_SHORT).show();
  //     startActivity(new Intent(MainActivity.this, NetworkEnvironmentActivity.class));
  //   }
  // }

  /**
   * 访问网络
   */
  private void requestNetwork() {
    NetworkApi.createService(HttpService.class)
        .getWallPaper()
        .compose(NetworkApi.applySchedulers(new BaseObserver<WallPaperResponse>() {
          @Override
          public void onSuccess(WallPaperResponse wallPaperResponse) {
            List<WallPaperResponse.ResBean.VerticalBean> vertical = wallPaperResponse.getRes().getVertical();
            if (vertical != null && vertical.size() > 0) {
              String imgUrl = vertical.get(0).getImg();
              // Glide.with(MainActivity.this).load(imgUrl).into(binding.image);
            } else {
              Toast.makeText(MainActivity.this, "数据为空", Toast.LENGTH_SHORT).show();
            }
          }

          @Override
          public void onFailure(Throwable e) {
            KLog.e("MainActivity", e.toString());
            Toast.makeText(MainActivity.this, "访问失败", Toast.LENGTH_SHORT).show();
          }
        }));
  }




}